<?php

namespace addons\qingdongams\model;

use app\common\model\Workorder as WorkorderModel;
use think\Exception;
use think\Model;
use traits\model\SoftDelete;

/**
 *添加物流信息
 */
class WorkorderLogistics extends Model
{
    use SoftDelete;

    protected $name  = 'qingdongams_workorder_logistics';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';


    public function getCreatetimeAttr($value)
    {
        return date('Y-m-d H:i', $value);
    }
    public function getContentAttr($value)
    {

       return json_decode($value, true);
    }
    //物流状态
    public function getCurrentStatusAttr($value)
    {
        return json_decode($value,true);
    }

    //
    public function getFileidsAttr($value) {
        $files  = explode(',', $value);
        $result = [];
        foreach ($files as $fid) {
            if ($fid) {
                $result[]=['url'=> cdnurl(File::getUrl($fid), true),'id'=>$fid] ;
            }
        }

        return $result;
    }


    //创建人
    public function createStaff()
    {
        return $this->hasOne(Staff::class, 'id', 'create_staff_id')->field('id,name');
    }



    //添加发货记录
    public static function createLogistics($params) {

        $staff                     = Staff::info();
        $params['create_staff_id'] = $staff->id;
        $model                     = new self;
        // 调用当前模型对应的User验证器类进行数据验证
        $result = $model->allowField(true)->save($params);
        $lastId = $model->getLastInsID();
        if($params['type'] == '送货上门' && $params['money']>0){
            $mileage_consume = [
                'title' =>  '送货上门-物流费用',
                'number' =>  Consume::getNumber(),
                'customer_id' => $params['customer_id'],
                'relation_id' => $params['workorder_id'],
                'relation_type' => Consume::WORKORDER_TYPE,
                'submit_date' => date('Y-m-d'),
                'remark'=>'',
                'money'=>$params['money']
            ];
            $mileage_consume['detail'][] = [
                'consume_date' => date('Y-m-d'),
                'consume_type' => '配送费',
                'car_type' => $params['company'],
                'consume_money' => $params['money'],
                'file_ids' => $params['file_ids']
            ];
            Consume::createConsume($mileage_consume);
        }
        $product_ids=explode(',',$params['product_ids']);
        CustomerProduct::where(['id'=>['in',$product_ids]])
            ->update(['status'=>1]);

        if (false === $result) {
            // 验证失败 输出错误信息
            throw new Exception($model->getError());
        }

        return true;
    }
    //修改发货记录
    public static function updateLogistics($params) {

        $staff                     = Staff::info();
        $model                     = new self;
        $row=$model->get($params['id']);
        if($row['type'] != '送货上门' && $params['type'] == '送货上门' && $params['money']>0){
            $mileage_consume = [
                'title' =>  '送货上门-物流费用',
                'number' =>  Consume::getNumber(),
                'customer_id' => $params['customer_id'],
                'relation_id' => $params['workorder_id'],
                'relation_type' => Consume::WORKORDER_TYPE,
                'submit_date' => date('Y-m-d'),
                'remark'=>'',
                'money'=>$params['money']
            ];
            $mileage_consume['detail'][] = [
                'consume_date' => date('Y-m-d'),
                'consume_type' => '配送费',
                'car_type' => $params['company'],
                'consume_money' => $params['money'],
                'file_ids' => $params['file_ids']
            ];
            Consume::createConsume($mileage_consume);
        }

        // 调用当前模型对应的User验证器类进行数据验证
        $result = $model->allowField(true)->save($params,['id'=>$params['id']]);
        $lastId = $model->getLastInsID();

        if (false === $result) {
            // 验证失败 输出错误信息
            throw new Exception($model->getError());
        }

        return true;
    }

}
